package com.lsh.interceptor;

import com.lsh.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IndexInterceptor implements HandlerInterceptor {

    @Autowired
    LoginService loginService;

    /**
     * 检查请求是否带token参数且token是否有效
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //访问页面拦截检查
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie: cookies){
                if(cookie.getName().equals("TOKEN")){
                    String token = cookie.getValue();
                    if(token != null || token != ""){
                        return true;
                    }
                }
            }
        }
        response.sendRedirect("/index");
        return false;
    }
}
